import {ElMessage} from "element-plus";
import {httpRequest} from "./httpRequest";
import store from "@/store/index";

// 分片上传
const uploadFile = async (file, index, closeSuccessMsg) => {
    return new Promise((resolve, reject) => {
        if (!index) index = 0
        if (!file) {
            reject('请选择文件！')
            return ElMessage.warning('请选择文件！')
        }
        // 文件分片
        let chunkSize = 1024 * 500; // 500KB 500KB Section size
        // [ 文件名, 文件后缀 ]
        const [fname, suffix] = file.name.split('.');
        // 获取当前片的起始字节
        const start = index * chunkSize;
        const blob = file.slice(start, start + chunkSize);
        // 为每片进行命名： 源文件名+索引+后缀+分片大小size+大文件总size(用与计算进度)
        const blobName = `${fname}.${index}.${suffix}.${chunkSize}.${file.size}`;
        const blobFile = new File([blob], blobName);
        const formData = new FormData();
        formData.append('file', blobFile);
        httpRequest("POST", "/uploadFile", null, formData, "proxy").then(res => {
            if (res?.data?.code === 200) {
                store.commit('SET_PROCESS', {name: fname, progress: res?.data?.progress})
                if (res?.data?.end) {
                    if (!closeSuccessMsg) ElMessage.success(res?.data?.msg)
                    store.commit('SET_UPLOAD_URL', {name: fname, url: res?.data?.url})
                    resolve(res)
                } else {
                    resolve(uploadFile(file, ++index, closeSuccessMsg))
                }
            } else if (res?.data?.code === 202) {
                // 202 上传暂停
                ElMessage.success(res?.data?.msg)
            } else {
                ElMessage.error(res?.data?.msg)
            }
        })
    })
}

// 暂停上传
const stopUploadFile = async (file) => {
    return new Promise((resolve, reject) => {
        if (!file) return ElMessage.warning('请选择文件！')
        let params = {name: file.name}
        httpRequest("POST", "/uploadPaused", null, params, "proxy", true,).then(res => {
            if (res?.data?.code === 500) {
                ElMessage.error(res?.data?.msg)
                reject(res)
            } else {
                ElMessage.success(res?.data?.msg)
                resolve(res)
            }
        })
    })
}
// 验证文件是否上传过
const verifyFile = async (file) => {
    return new Promise((resolve, reject) => {
        if (!file) return ElMessage.warning('请选择文件！')
        let params = {name: file.name}
        httpRequest("POST", "/verify", null, params, "proxy", true,).then(res => {
            if (res?.data?.code === 500) {
                ElMessage.error(res?.data?.msg)
                res.data.file = file
                reject(res)
            } else {
                resolve(res)
            }
        })
    })
}
// 断点续传
const continueUploadFile = async (file) => {
    const res = await verifyFile(file);
    if (res?.data?.code === 201) {
        await uploadFile(file, res?.data?.data?.length)
    } else {
        ElMessage.info(res?.data?.msg)
    }
}
export {
    uploadFile,
    stopUploadFile,
    continueUploadFile
}